/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.monkey.core.domain.organization;

import com.monkey.core.domain.BaseEntity;
import com.monkey.core.plugin.domain.Status;
import com.monkey.core.plugin.domain.Treeable;
import com.monkey.core.plugin.dto.MonkeyDto;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Formula;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 组织机构树
 */
@Entity
@Table(name = "sys_organization")
@DynamicUpdate
@DynamicInsert
public class Organization extends BaseEntity<Long> implements Treeable<Long, Organization> {

    /**
     * 标题
     */
    private String name;

    /**
     * 组织机构类型 默认 公司
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "type_")
    private OrganizationType type = OrganizationType.BRANCH_OFFICE;
    /**
     * 父路径
     */
    @Column(name = "parent_id")
    private Long parentId;

    @Column(name = "parent_ids")
    private String parentIds;

    private Integer weight;

    /**
     * 图标
     */
    private String icon;

    /**
     * 是否子节点
     */
    @Formula(value = "(select count(*) from sys_organization f_t where f_t.parent_id = id)")
    private boolean hasChildren;
    private boolean expand;

    @Enumerated(EnumType.STRING)
    private Status status;

    @Transient
    private List<Organization> children = new ArrayList<>();


    public Organization() {
    }

    public Organization(Long id) {
        setId(id);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public OrganizationType getType() {
        return type;
    }

    public void setType(OrganizationType type) {
        this.type = type;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    @Override
    public String makeSelfAsNewParentIds() {
        return getParentIds() + getId() + getSeparator();
    }

    @Override
    public String getSeparator() {
        return DEFAULT_SEPARATOR;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getIcon() {
        if (!StringUtils.isEmpty(icon)) {
            return icon;
        }
        if (isRoot()) {
            return getRootDefaultIcon();
        }
        if (isLeaf()) {
            return getLeafDefaultIcon();
        }
        return getBranchDefaultIcon();
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }


    @Override
    public boolean isRoot() {
        return getParentId() != null && getParentId() == 0;
    }

    @Override
    public boolean isLeaf() {
        return !isRoot() && !isHasChildren();

    }

    @Override
    public boolean isHasChildren() {
        return hasChildren;
    }

    public void setHasChildren(boolean hasChildren) {
        this.hasChildren = hasChildren;
    }

    /**
     * 根节点默认图标 如果没有默认 空即可
     */
    @Override
    public String getRootDefaultIcon() {
        return Treeable.DEFAULT_ROOT_ICON_CLS;
    }

    /**
     * 树枝节点默认图标 如果没有默认 空即可
     */
    @Override
    public String getBranchDefaultIcon() {
        return Treeable.DEFAULT_BRANCH_ICON_CLS;
    }

    /**
     * 树叶节点默认图标 如果没有默认 空即可
     */
    @Override
    public String getLeafDefaultIcon() {
        return Treeable.DEFAULT_LEAF_ICON_CLS;
    }


    @Override
    public boolean isExpand() {
        return this.expand;
    }

    @Override
    public void setExpand(boolean expand) {
        this.expand = expand;
    }

    @Override
    public Status getStatus() {
        return status;
    }

    @Override
    public void setStatus(Status status) {
        this.status = status;
    }

    @Override
    public List<Organization> getChildren() {
        return this.children;
    }

    @Override
    public void addChild(Organization child) {
        this.children.add(child);
    }

    public static Organization createFromMonkeyDto(MonkeyDto monkeyDto) {
        Organization organization = new Organization();
        organization.setName(monkeyDto.getString("name"));
        organization.setStatus(Status.valueOf(monkeyDto.getString("status")));
        organization.setExpand(monkeyDto.getBoolean("expand"));
        organization.setWeight(monkeyDto.getInteger("weight"));
        organization.setIcon(monkeyDto.getString("icon"));
        organization.setParentId(monkeyDto.getLong("parentId"));
        organization.setType(OrganizationType.valueOf(monkeyDto.getString("type")));
        return organization;
    }

    public static void modifyFromMonkeyDto(Organization organization, MonkeyDto monkeyDto) {
        organization.setName(monkeyDto.getString("name"));
        organization.setStatus(Status.valueOf(monkeyDto.getString("status")));
        organization.setExpand(monkeyDto.getBoolean("expand"));
        organization.setWeight(monkeyDto.getInteger("weight"));
        organization.setIcon(monkeyDto.getString("icon"));
        organization.setParentId(monkeyDto.getLong("parentId"));
        organization.setType(OrganizationType.valueOf(monkeyDto.getString("type")));
    }
}
